<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png?v=5.1.4">


  <link rel="mask-icon" href="/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="数据结构,算法,数据结构与算法,时间复杂度,">










<meta name="description" content="时间复杂度是学习数据结构与算法的基础，想要评估算法的好坏，就需要使用时间复杂度和空间复杂度来度量，通过本文来探究时间复杂度的真身吧">
<meta name="keywords" content="数据结构,算法,数据结构与算法,时间复杂度">
<meta property="og:type" content="article">
<meta property="og:title" content="数据结构与算法01-时间复杂度">
<meta property="og:url" content="https://sakurayuki.gitee.io/post/5902f98d.html">
<meta property="og:site_name" content="Yukiの小居">
<meta property="og:description" content="时间复杂度是学习数据结构与算法的基础，想要评估算法的好坏，就需要使用时间复杂度和空间复杂度来度量，通过本文来探究时间复杂度的真身吧">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://sakurayuki.gitee.io/static/img/DataStructuresAlgorithms/求和函数1-5-10.png">
<meta property="og:image" content="https://sakurayuki.gitee.io/static/img/DataStructuresAlgorithms/求和函数1-1k-100w-10e-100e.png">
<meta property="og:image" content="https://sakurayuki.gitee.io/static/img/DataStructuresAlgorithms/9x9Function-Run-Result.png">
<meta property="og:image" content="https://sakurayuki.gitee.io/static/img/FaceBag/写的什么东西.jpg">
<meta property="og:image" content="https://sakurayuki.gitee.io/static/img/DataStructuresAlgorithms/Comparison-Computational-Complexity.png">
<meta property="og:image" content="https://sakurayuki.gitee.io/static/img/DataStructuresAlgorithms/Merge-Sort-Example.gif">
<meta property="og:updated_time" content="2019-01-24T06:14:06.452Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="数据结构与算法01-时间复杂度">
<meta name="twitter:description" content="时间复杂度是学习数据结构与算法的基础，想要评估算法的好坏，就需要使用时间复杂度和空间复杂度来度量，通过本文来探究时间复杂度的真身吧">
<meta name="twitter:image" content="https://sakurayuki.gitee.io/static/img/DataStructuresAlgorithms/求和函数1-5-10.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://sakurayuki.gitee.io/post/5902f98d.html">





  <title>数据结构与算法01-时间复杂度 | Yukiの小居</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Yukiの小居</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">浮华都市 坚定向前</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://sakurayuki.gitee.io/post/5902f98d.html">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Yuki">
      <meta itemprop="description" content="智人发明了语言 智人发明了编程语言">
      <meta itemprop="image" content="/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Yukiの小居">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">数据结构与算法01-时间复杂度</h2>
        

        <div class="post-meta">
          <span class="post-time">
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/数据结构与算法/" itemprop="url" rel="index">
                    <span itemprop="name">数据结构与算法</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/post/5902f98d.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="post/5902f98d.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i> 本文阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                <span title="字数统计">
                  2.8k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                <span title="阅读时长">
                  10
                </span>
              
            </div>
          

          
              <div class="post-description">
                  时间复杂度是学习数据结构与算法的基础，想要评估算法的好坏，就需要使用时间复杂度和空间复杂度来度量，通过本文来探究时间复杂度的真身吧
              </div>
          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>山高不高用海拔来比较，猪重不重用重量来比较，那代码快不快用什么来比较呢</p>
</blockquote>
<h2 id="什么是时间复杂度"><a href="#什么是时间复杂度" class="headerlink" title="什么是时间复杂度"></a>什么是时间复杂度</h2><blockquote>
<p>时间复杂度是一个理想指标，用于丈量代码运行效率，它描述的不是具体的时间长短，而是随着数据的变化，代码运行所需时间的一个趋势</p>
</blockquote>
<h3 id="不关心具体"><a href="#不关心具体" class="headerlink" title="不关心具体"></a>不关心具体</h3><p>时间复杂度也叫渐进时间复杂度，什么叫渐进呢？用最直观的代码举例</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params">num</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (num &lt; <span class="number">0</span>) &#123;</span><br><span class="line">    num = num * <span class="number">-1</span>;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    num = num * num;</span><br><span class="line">  &#125;</span><br><span class="line">  num = num * <span class="number">0.5</span>;</span><br><span class="line">  <span class="keyword">return</span> num;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上列代码是一个算法，如果是负数就取绝对值，否则取平方，然后乘以0.5。<br>可以看到代码都要执行3个步骤</p>
<ol>
<li>判断正负</li>
<li>取绝对值或者平方</li>
<li>乘以0.5</li>
</ol>
<p>假设每个步骤的用时是相等的，都是 1，它的时间复杂度是多少呢？<br>如果按照数学思维，那就是 3 对不对，是对的。那么问题来了，这是个简单的方法，我们算出是 3 了，那下次 100 行的代码还要一个一个去算啊，这就很不科学对吧，还有那么多 if else，所以我们需要<strong>不关心具体</strong>，脱离具体去求一个渐进时间复杂度的就好。</p>
<h3 id="CPU视角"><a href="#CPU视角" class="headerlink" title="CPU视角"></a>CPU视角</h3><p>也许 10000 次计算和 1次计算在我们眼里有天壤之别，但是在CPU看来，实在是微不足道，但要是执行 n 计算和 10000次计算相比，CPU 肯定愿意直接执行 10000 次计算来得稳，谁知道 n 次到底是 100 次还是 1亿次呢。</p>
<p>所以只要是常量，不管是 1 次，10 次，还是 10 亿次的计算，他们的渐进时间复杂度(后面都简称时间复杂度)都是一样的。</p>
<p>这里介绍第一个时间复杂度：$O(1)$</p>
<p>大写 O 表示的是代码执行时间与括号中的表达式成正比，因此这部分是不变的，只需关心 O 中的的表达式内容即可。</p>
<p>这里提出的时间复杂度 $O(1)$ 是指那些代码执行次数为常数的方法，不管方法执行 10 次，1000 次还是 10 亿次，都判定为 $O(1)$ 的时间复杂度。</p>
<h3 id="循环的威力"><a href="#循环的威力" class="headerlink" title="循环的威力"></a>循环的威力</h3><p>循环（<code>for</code>、<code>while</code>）是编程最常用到的功能，我学 Java 时接触的第一个循环的内容就是双循环打印九九乘法表，对初学者来说一次性搞定不大可能，我重试了几十次才打印出来争取的效果。</p>
<p>像求和函数，就是把数组中的数循环累加；求最大值，也是循环数组依次对比，找出最大的数。</p>
<p>因为不确定数组的长度，所以像带有循环的方法，时间复杂度并不是 $O(1)$，未知规模一般认为是 n，将 n 带入公式来计算时间复杂度，求和函数的时间复杂度就是 $O(n)$</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> total = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">        total = total + arr[i];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> total;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>来分析这个简单的求和函数</p>
<ul>
<li>声明 <code>total</code> 语句执行了一次</li>
<li>声明 <code>i</code> 语句执行了一次</li>
<li>判断数组长度与 <code>i</code> 的大小、<code>i++</code>和累加操作分别执行了 n 次</li>
</ul>
<p>(判断数组长度与 <code>i</code> 的大小会多 1 次，但为了简单这里我们忽略这个)</p>
<p>所以这个方法时间复杂度算下来应该是 $O(2 + 3n)$ ，这是没有问题的，不过大家在研究时间复杂度的时候，一般都只保留最大量级的那一项，在这个时间复杂度中，常数 2 是不左右增长趋势的，可以排除，然后系数 3 同样不会影响增长趋势，所以最终的时间复杂度是 $O(n)$。</p>
<img src="/static/img/DataStructuresAlgorithms/求和函数1-5-10.png" title="n 在1、5、10下不同时间复杂度的运行代码次数">
<p>可以看到当 n 的规模很小时，常数 2 还能产生一点影响，但当 n 的规模放大时，常数已经看不到任何影响了。</p>
<img src="/static/img/DataStructuresAlgorithms/求和函数1-1k-100w-10e-100e.png" title="n 在1、1k、100w、10e、100e 下不同时间复杂度的运行代码次数">
<p>在 n 的规模很大时，$O(3n)$ 和 $O(n)$ 达到了相同的增长趋势，系数 3 已经不会影响增长趋势，因此可以忽略。</p>
<h3 id="打印九九乘法表"><a href="#打印九九乘法表" class="headerlink" title="打印九九乘法表"></a>打印九九乘法表</h3><p>对于新手来说，打印九九乘法表真的很不容易，因为循环嵌套很容易绕晕新手，那时候同学说的最多的话就是“为啥我打印出来不是三角形的”，哈哈，第二个循环的初始条件设置错了。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">1</span>; i &lt;= <span class="number">9</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">let</span> line = <span class="string">''</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> j = i; j &lt;= <span class="number">9</span>; j++) &#123;</span><br><span class="line">            line = <span class="string">`<span class="subst">$&#123;line&#125;</span><span class="subst">$&#123;i&#125;</span> * <span class="subst">$&#123;j&#125;</span> = <span class="subst">$&#123;i * j&#125;</span>  `</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">console</span>.log(line);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我用 JavaScript 写了这样一个打印九九乘法表的方法，为什么我用 JavaScript 写呢？因为看文章的时候可以直接复制在控制台执行，比较直观。</p>
<img src="/static/img/DataStructuresAlgorithms/9x9Function-Run-Result.png" title="打印九九乘法表函数运行效果">
<p>这个方法的时间复杂度怎么算呢？</p>
<p>其实不用算可以直接得到，是 $O(1)$，因为这里是打印 9x9 的乘法表，所以其实是常数，根据前面的知识，常数的时间复杂度都是 $O(1)$。</p>
<p>哈哈哈，没想到吧！</p>
<img src="/static/img/FaceBag/写的什么东西.jpg" width="200">
<p>好吧，实际上我们只要改一下输入，让输入规模不定死在 9，就可以打印千千乘法表，万万乘法表，甚至亿亿乘法表（误）。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params">n</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</span><br><span class="line">        <span class="keyword">let</span> line = <span class="string">''</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> j = i; j &lt;= n; j++) &#123;</span><br><span class="line">            line = <span class="string">`<span class="subst">$&#123;line&#125;</span><span class="subst">$&#123;i&#125;</span> * <span class="subst">$&#123;j&#125;</span> = <span class="subst">$&#123;i * j&#125;</span>  `</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">console</span>.log(line);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>改造完就可以自由输入数据规模了，当然这是为了说明问题，实际上这个方法貌似没啥用。</p>
<p>那么，这个方法的时间复杂度是多少呢？</p>
<p>首先外层的循环是肯定要执行的，<code>n</code> 输入的是多少，外层循环就执行多少次，也就是 n 次，然后内层循环的执行次数随着外层循环次数依次由 <code>n</code> 递减至 <code>1</code>，最后结束程序。</p>
<p>其实看图就可以看出来，是 n * n 的一半，也就是 $O({ {1 \over 2} n ^ 2})$。</p>
<p>然后这里的系数可以忽略，最终时间复杂度就是 $O({ n ^ 2})$。</p>
<p>这就是本文接触的第三种时间复杂度，人们常说的冒泡排序，时间复杂度就是这个了，这个时间复杂度其实已经很高了，想想 1000w，1e 的平方，那真的是很大很大呀，想想就刺激。所以冒泡排序是一种非常低效的排序，后面我们会学习好些个的排序，通过时间复杂度，就可以来比较评估它们的运行效率，所以学会时间复杂度对数据结构与算法的学习，是非常重要的。</p>
<h2 id="常见的时间复杂度"><a href="#常见的时间复杂度" class="headerlink" title="常见的时间复杂度"></a>常见的时间复杂度</h2><blockquote>
<p>时间复杂度并没有那么“复杂”，常见的时间复杂度就只有几种，只要掌握它们的特征，分析起来其实很简单</p>
</blockquote>
<h3 id="高阶时间复杂度是噩梦"><a href="#高阶时间复杂度是噩梦" class="headerlink" title="高阶时间复杂度是噩梦"></a>高阶时间复杂度是噩梦</h3><p>随着数据规模的增长，有些时间复杂度的增长是极为可怕的，这样的算法基本是没法用的，常见的时间复杂度随数据量增长趋势如下图</p>
<img src="/static/img/DataStructuresAlgorithms/Comparison-Computational-Complexity.png" title="常见函数的时间复杂度(来自维基百科)">
<p>可以看到，$O(n!)$ 和 $O(2^n)$ 的增长是非常夸张的，基本凉了，数据量一旦增大，至强 CPU 也带不动啊。</p>
<p>所以我们一般接触的都是 $O(n^2)$ 这个量级以内的算法（也包括 $O(n^3)$ 这些更高阶的，极少），因为这样的算法还有实践的意义。</p>
<h3 id="常见时间复杂度简介"><a href="#常见时间复杂度简介" class="headerlink" title="常见时间复杂度简介"></a>常见时间复杂度简介</h3><p>$O(1)$、$O(n)$ 和 $O(n^2)$ 我们已经说过了，常用的还有 $O(\log n)$ 和 $O(n \log n)$，这两种相对来说比较绕一点，没关系，只要掌握特征，一样可以分析出来的。</p>
<p>先说说 $O(\log n)$，这里我就不举例代码了，我说一个很有意思的故事，相信你听完比看代码更能理解，顺带还能学一种算法。</p>
<p>小美每天都会在朋友圈放一张自拍照，由于颜值不错很多朋友点赞。直到有一天，闺蜜告诉她在某社交网站上看到了她的照片，妹子听后非常吃惊，也非常生气，自己只发朋友圈，这肯定是盗图。那怎么才能找出这个人呢？这人应该在微信好友里，有500多好友，怎么找呢？</p>
<p>闺蜜之前加入了一个程序员众多的社群，便向社群里求助，然后群里聊得热火朝天，争先恐后的献计，很快就有人给出了很高效的解决方案：</p>
<blockquote>
<p>二分查找法，现对微信好友分两组，第一天发照片时设置对其中的一组（A组）可见，对另一组（B组）不可见，然后观察这个社交网站，如果有相同照片，说明贼在 A 组，否则在 B 组，第二天在贼所在的组继续进行二分，第三天继续，这样第九天就可以找到那个贼（$2^9$=512）。<br>还有提出优化方案的，分三组，每次对其中两组发两张不同照片，或者分四组，极端情况下分 500 组，发 499 张不同照片，当天就可以找出这个人。</p>
</blockquote>
<p>妹子最后使用四分查找法在第5天就找到了那个偷照片的贼，痛骂了一番，然后怒删之。</p>
<p>这里的二分查找，其时间复杂度就是 $O(\log _2 n)$，当然，如果分为四组，那就是 $O(\log _4 n)$，这里的底数 2 和 4 都是常数，因此可以省略，最终的时间复杂度是 $O(\log n)$</p>
<p>那么 $O(n \log n)$ 又是怎么回事呢？比较有代表性的就是<strong>归并排序</strong>，归并排序的时间复杂度就是 $O(n \log n)$。</p>
<p>下图展示了归并排序的原理，如果不理解也没有关系，后面有专门的章节来讲述这些排序算法，现在就先记住就好，记不住也不打紧。</p>
<img src="/static/img/DataStructuresAlgorithms/Merge-Sort-Example.gif" title="使用归并排序为一列数字进行排序的过程(来自维基百科)">
<h2 id="写在最后"><a href="#写在最后" class="headerlink" title="写在最后"></a>写在最后</h2><p>本想着一星期写一篇文章，结果发现自己文笔真的很烂，而且对如何描述自己的想法也有很多考虑，年末事情也多，MathJax 在 Markdown 里写公式也折腾了我一下午，这篇文章在 1 月 7 日 <code>hexo post new</code>，结果 1 月 24 日才初步写完，真的是惭愧。</p>
<p>然后发现时间复杂度好像一篇讲不完，但也不想写两篇了，后面写在在需要出现的地方吧，比较贴合实际。</p>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><p><a href="https://time.geekbang.org/column/126" target="_blank" rel="noopener">数据结构与算法之美 - 极客时间</a><br><a href="https://zh.wikipedia.org/wiki/时间复杂度" target="_blank" rel="noopener">时间复杂度 - 维基百科</a><br><a href="https://zh.wikipedia.org/wiki/归并排序" target="_blank" rel="noopener">归并排序 - 维基百科</a><br><a href="https://zhuanlan.zhihu.com/p/54069748" target="_blank" rel="noopener">二分法解决妹子遇到的难题 - 郑征 - 知乎</a></p>

      
    </div>
    
    
    

    

    

    
      <div>
        <ul class="post-copyright">

  
    <li class="post-copyright-author">
      <strong>文章标题：</strong>
      数据结构与算法01-时间复杂度
    </li>
  
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    Yuki
  </li>
  
    <li class="post-copyright-author">
      <strong>发布时间：</strong>
      <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-01-07T09:49:14+08:00">
        2019年01月07日 09:49
      </time>
    </li>
  
  
    <li class="post-copyright-author">
      <strong>更新时间：</strong>
      <time title="更新于" itemprop="dateModified" datetime="2019-01-24T14:14:06+08:00">
        2019年01月24日 14:14
      </time>
    </li>
  
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://sakurayuki.gitee.io/post/5902f98d.html" title="数据结构与算法01-时间复杂度">https://sakurayuki.gitee.io/post/5902f98d.html</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    <i class="fa fa-creative-commons"></i> <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/数据结构/" rel="tag"><i class="fa fa-tag"></i> 数据结构</a>
          
            <a href="/tags/算法/" rel="tag"><i class="fa fa-tag"></i> 算法</a>
          
            <a href="/tags/数据结构与算法/" rel="tag"><i class="fa fa-tag"></i> 数据结构与算法</a>
          
            <a href="/tags/时间复杂度/" rel="tag"><i class="fa fa-tag"></i> 时间复杂度</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/post/a830df26.html" rel="next" title="数据结构与算法00-缘起">
                <i class="fa fa-chevron-left"></i> 数据结构与算法00-缘起
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/avatar.gif" alt="Yuki">
            
              <p class="site-author-name" itemprop="name">Yuki</p>
              <p class="site-description motion-element" itemprop="description">智人发明了语言 智人发明了编程语言</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="mailto:yuki1015@live.com" target="_blank" title="Email">
                      
                        <i class="fa fa-fw fa-envelope"></i>Email</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/sakurayuki" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://twitter.com/YukiSakura_K" target="_blank" title="Twitter">
                      
                        <i class="fa fa-fw fa-twitter"></i>Twitter</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/yuki951015" target="_blank" title="Weibo">
                      
                        <i class="fa fa-fw fa-weibo"></i>Weibo</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/yuki_sakura" target="_blank" title="Zhihu">
                      
                        <i class="fa fa-fw fa-quora"></i>Zhihu</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#什么是时间复杂度"><span class="nav-number">1.</span> <span class="nav-text">什么是时间复杂度</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#不关心具体"><span class="nav-number">1.1.</span> <span class="nav-text">不关心具体</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#CPU视角"><span class="nav-number">1.2.</span> <span class="nav-text">CPU视角</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#循环的威力"><span class="nav-number">1.3.</span> <span class="nav-text">循环的威力</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#打印九九乘法表"><span class="nav-number">1.4.</span> <span class="nav-text">打印九九乘法表</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#常见的时间复杂度"><span class="nav-number">2.</span> <span class="nav-text">常见的时间复杂度</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#高阶时间复杂度是噩梦"><span class="nav-number">2.1.</span> <span class="nav-text">高阶时间复杂度是噩梦</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#常见时间复杂度简介"><span class="nav-number">2.2.</span> <span class="nav-text">常见时间复杂度简介</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#写在最后"><span class="nav-number">3.</span> <span class="nav-text">写在最后</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#参考"><span class="nav-number">4.</span> <span class="nav-text">参考</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Yuki</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-pencil"></i>
    </span>
    
    <span title="博客文字总数">5.5k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a></div>




        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-eercast"></i> 总访客
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i> 总访问
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>



<script>
  	var _mtac = {};
  	(function() {
  		var mta = document.createElement("script");
  		mta.src = "https://pingjs.qq.com/h5/stats.js?v2.0.4";
  		mta.setAttribute("name", "MTAH5");
  		mta.setAttribute("sid", "500665537");

  		var s = document.getElementsByTagName("script")[0];
  		s.parentNode.insertBefore(mta, s);
  	})();
</script>



  <script type="text/javascript">
    (function() {
      var hm = document.createElement("script");
      hm.src = "//tajs.qq.com/stats?sId=66153766";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  

    
      <script id="dsq-count-scr" src="https://sakurayuki-gitee-io.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'https://sakurayuki.gitee.io/post/5902f98d.html';
          this.page.identifier = 'post/5902f98d.html';
          this.page.title = '数据结构与算法01-时间复杂度';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://sakurayuki-gitee-io.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  

















  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>

   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;"></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/src/fireworks.js"></script>

</html>
